import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

public class GenerateFile {

	public static String[] fixedResults;

	/**
	 * read and store the results passed from the GUI
	 * @param results
	 */
	public static void readResults(String[] results) {
		fixedResults = results;
		convertMonth();
		}


	/**
	 * write the file
	 * @throws FileNotFoundException
	 */
	public static void write(int mode)	throws FileNotFoundException {
		PrintWriter writer;

		if (fixedResults[0].length() > 0) {
			writer = new PrintWriter(fixedResults[0] + ".ics");
		} else {
			writer = new PrintWriter("BlankEvent.ics");
		}
		String DTSTART = ("DTSTART;TZID=" + fixedResults[18] + ":");
		String DTEND = ("DTEND;TZID=" + fixedResults[18] + ":");

		if (mode == 1) {
			Calendar now = Calendar.getInstance();
			String date = now.get(Calendar.YEAR) + leadingZeros(now.get(Calendar.MONTH) + 1) + leadingZeros(now.get(Calendar.DATE));
	
			
			DTSTART += date +"T" + fixedResults[5] + fixedResults[6] + "00";
			DTEND += date + "T" + fixedResults[10] + fixedResults[11] + "00";
		
		} else {
			DTSTART += fixedResults[4] + fixedResults[2] + fixedResults[3] + 
					"T" + fixedResults[5] + fixedResults[6] + "00";
			DTEND += fixedResults[9] + fixedResults[7] + fixedResults[8] + 
					"T" + fixedResults[10] + fixedResults[11] + "00";
		}

		String SUMMARY = ("SUMMARY:") + (fixedResults[0]);
		String LOCATION = ("LOCATION:") + (fixedResults[1]);
		String DESCRIPTION = ("DESCRIPTION:") + (fixedResults[12]);
		String PRIORITY = ("PRIORITY:") + (fixedResults[14]);
		String CLASSIFICATION = ("CLASS:") + (fixedResults[13]);

		DTSTART = limitLines(DTSTART);
		DTEND = limitLines(DTEND);
		SUMMARY = limitLines(SUMMARY);
		LOCATION = limitLines(LOCATION);
		DESCRIPTION = limitLines(DESCRIPTION);
		PRIORITY = limitLines(PRIORITY);
		CLASSIFICATION = limitLines(CLASSIFICATION);

		writer.println("BEGIN:VCALENDAR");
		writer.println("VERSION:2.0");
		writer.println("BEGIN:VEVENT");
		writer.println(DTEND);
		writer.println(DTSTART);
		writer.println(SUMMARY);
		writer.println(LOCATION);
		writer.println(DESCRIPTION);
		writer.println(PRIORITY);
		writer.println(CLASSIFICATION);
		if (mode == 1) {
			writer.println("RRULE:FREQ=WEEKLY;BYDAY=" + fixedResults[15]);
		}
		if (!fixedResults[16].equals("")) {
			writer.println("GEO:" + fixedResults[16] + "," + fixedResults[17]);
		}
		writer.println("END:VEVENT");
		writer.println("END:VCALENDAR");

		writer.close();
	}

	private static String limitLines(String content) {
		if (content.length() > 75) {
			String temp = "";
			for (int i = 0; i < content.length() / 75; i ++) {
				temp += content.substring(75 * i, 75 * (i + 1)) + "\r\n" + " ";
			}
			return temp;
		}
		return content;
	}


	/**
	 * Convert from string to numerical months
	 */
	public static void convertMonth() {
		if (fixedResults[2] != null) {
			int j = 2;
			for (int i = 0; i < 2; i ++) {		
				switch (fixedResults[j]) {
				case "Jan": fixedResults[j] = "01";
				break;	
				case "Feb": fixedResults[j] = "02";
				break;
				case "Mar": fixedResults[j] = "03";
				break;
				case "Apr": fixedResults[j] = "04";
				break;
				case "May": fixedResults[j] = "05";
				break;
				case "Jun": fixedResults[j] = "06";
				break;
				case "Jul": fixedResults[j] = "07";
				break;
				case "Aug": fixedResults[j] = "08";
				break;
				case "Sep": fixedResults[j] = "09";
				break;
				case "Oct": fixedResults[j] = "10";
				break;
				case "Nov": fixedResults[j] = "11";
				break;
				case "Dec": fixedResults[j] = "12";
				break;
				default: if (fixedResults[j].length() != 2) {
					String temp = "0" + fixedResults[j];
					fixedResults[j] = temp;
				}
				}
				j = 7;
			}
		}
	}

	
	private static String leadingZeros(int num) {
		String temp = "";
		if (num < 10) {
			temp += "0" + num;
		} else {
			temp += num;
		}
		
		return temp;
	}

}